package cc.messcat.web.fupin;

import cc.messcat.common.constant.CommonConstant;
import cc.messcat.common.model.Pager;
import cc.messcat.common.model.ResponseBean;
import cc.messcat.entity.fupin.Vision;
import cc.messcat.service.fupin.VisionService;
import cc.messcat.service.system.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 街景控制层
 */
@Controller
@RequestMapping("/vision")
public class VisionController {
	static Logger log = LoggerFactory.getLogger(VisionController.class);

	@Autowired
	private UserService userService;

	@Autowired
	private VisionService visionService;

    /**
     * 添加街景
     */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public Object addVision(Vision vision) {
		vision.setCreateTime(new Date());
		visionService.addObject(vision);
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_ADD_SUCCESS);
	}


    /**
     * 根据id获取街景
     */
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	public Object getVision(@PathVariable Long id) {
        Vision vision = visionService.retrieveObject(id);
        return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS,vision);
	}

    /**
     * 根据所属村id和上传日期获取街景
     */
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Object listVisionByVillageID(@RequestParam("villageID") Long villageID,@RequestParam(name="date",required = false) String date,Integer pageNo,Integer pageSize) {
        List<Vision> visionList = null;
		if(pageNo == null || pageSize == null) {
			visionList = visionService.listVision(villageID, date,pageNo, pageSize);
			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS,visionList);
		} else {
			pageSize = pageSize < 1 ? 12 : pageSize;
			pageNo = pageNo < 1 ? 1 : pageNo;
			int rows = visionService.count(villageID,date);
			visionList = visionService.listVision(villageID, date,pageNo, pageSize);
			return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS,new Pager(pageSize,pageNo,rows,visionList));
		}
    }

	/**
	 * 根据所属村id获取所有街景上传日期
	 */
	@RequestMapping(value = "listDate",method = RequestMethod.GET)
	@ResponseBody
	public Object listVisionDateByVillageID(@RequestParam("villageID") Long villageID) {
		List<String> dateList = visionService.listVisionDateByVillageID(villageID);

		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_FIND_SUCCESS,dateList);

	}

    /**
     * 更新街景
     */
	@RequestMapping(method = RequestMethod.PUT)
	@ResponseBody
	public Object updateVision(Vision vision) {
	    if(vision.getId() == null)
	        return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_UPDATE_FAIL);

	    vision.setUpdateTime(new Date());

        visionService.modifyObject(vision);

		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_UPDATE_SUCCESS);
	}

    /**
     * 以id删除街景
     */
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public Object removeVision(@PathVariable Long id) {
	    if(id == null)
            return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_DELETE_FAIL);

		visionService.removeObject(id);
		return new ResponseBean(CommonConstant.SUCCESS_CODE_200, CommonConstant.MSG_DELETE_SUCCESS);
	}

}
